<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    聚游管理后台
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="verification">
                            <div style="height: 45px;">
                                <Input v-model="form.verification" style="width: 60%" placeholder="请输入验证码"
                                       @keyup.enter="handleSubmit">
                                <span slot="prepend">
                                    <Icon :size="24" type="key"></Icon>
                                </span>
                                </Input>
                                <img :src="verifyCodeUrl" id="code" style="position: relative; left: 245px; top: -44px;"
                                     @click="changeCode" alt="看不清楚，换一张"/>
                            </div>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" :loading="loading" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    import {mapActions} from 'vuex';
    import util from '@/libs/util.js';

    export default {
        data() {
            return {
                form: {
                    userName: 'admin',
                    password: '123456',
                    verification: ''
                },
                verifyCodeUrl: '',
                verifyCodeId: '',
                loading: false,
                rules: {
                    userName: [
                        {required: true, message: '账号不能为空', trigger: 'blur'},
                        {type: 'string', min: 4, message: '账号输入错误', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'},
                        {type: 'string', min: 6, message: '密码输入错误', trigger: 'blur'}
                    ],
                    verification: [
                        {required: true, message: '验证码不能为空', trigger: 'blur'},
                        {
                            type: 'string',
                            min: 4,
                            max: 4,
                            pattern: /^[A-Za-z0-9]{4}$/,
                            message: '验证码输入错误',
                            trigger: 'blur'
                        }
                    ]
                }
            };
        },
        created() {
            this.changeCode();
        },
        methods: {
            ...mapActions(['login', 'getVerificationCode', 'getOwnerMenu']),
            handleSubmit() {
                this.loading = true;
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.login({
                            username: this.form.userName,
                            password: this.form.password,
                            vercode: this.form.verification,
                            verifyCodeId: this.verifyCodeId
                        }).then(res => {
                            this.$router.push({
                                name: 'home_index'
                            });
                            util.initRouter(this);
                            this.loading = false;
                        }, error => {
                            this.changeCode();
                            this.loading = false;
                        });
                    }
                });
            },
            changeCode() {
                this.getVerificationCode().then(res => {
                    this.verifyCodeId = res.data.id;
                    this.verifyCodeUrl = res.data.img;
                    this.form.verification = '';
                });
            }
        }
    };
</script>

<style>

</style>
